﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ActionSystem.Core;
using XMLib;

namespace ActionSystem.Camera
{
    /// <summary>
    /// 头顶往下看
    /// </summary>
    [ActionConfig(typeof(TopDwonActionHandler))]
    public class TopDwonActionConfig : BaseStateActionConfig
    {
        [CustomLabel("距离跟随的距离")]
        public float distance = 20;
        [CustomLabel("相机右方")]
        public Vector3 right = Vector3.right;
        [CustomLabel("相机前方")]
        public Vector3 forward = Vector3.down;
        [CustomLabel("跟踪速度")]
        public float speed = 1;
    }
    public class TopDwonActionHandler : BaseCameraHander
    {
        TopDwonActionConfig config;
        Quaternion targetRotation;
        float baseSpeed = 0;
        public override void Enter(BaseStateAction action)
        {
            base.Enter(action);
            config = action.Config as TopDwonActionConfig;            
            targetRotation = Quaternion.LookRotation(config.forward, Vector3.Cross(config.forward, config.right));
            baseSpeed = config.speed / (1 + config.speed);//speed越大，速度越快
        }
        public override void Update(BaseStateAction action, float deltaTime)
        {
            if(follow==null||config==null)
            {
                return;
            }
            var targetposition = follow.GetFallowPostion() + config.distance * follow.GetFallowUpDirection();
            //更新相机位置
            float t  = baseSpeed * action.State.Weight;
            camera.transform.position = Vector3.Lerp(camera.transform.position,targetposition, t);
            //朝向
            camera.transform.rotation = Quaternion.Slerp(camera.transform.rotation,targetRotation,t);
        }
        public override void SetUpCameraInfo(BaseStateAction action, float deltaTime)
        {
            //更新ICameraInfo
            if (cameraMachine != null && cameraMachine.GetCamera() != null)
            {
                info?.SetMoveFarwrd(Vector3.Cross(follow.GetFallowUpDirection(), cameraMachine.GetCamera().transform.right));
                info?.SetMoveRight(cameraMachine.GetCamera().transform.right);
                info?.SetMoveUp(follow.GetFallowUpDirection());
            }
            else
            {
                info?.SetMoveFarwrd(Vector3.zero);
                info?.SetMoveRight(Vector3.zero);
                info?.SetMoveUp(Vector3.zero);
            }
        }
    }
}